using System;
using System.Collections.Generic;
using System.Text;
using sharpPDF;
using sharpPDF.Enumerators;

namespace BTV.Base.Printing
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1705:LongAcronymsShouldBePascalCased")]
    public class PDFPrinter : BTVPrinter
    {
        public PDFPrinter(List<BTVChannel> input) : base(input) { }

        private pdfDocument _pdfDoc;

        public pdfDocument PdfDoc
        {
            get { return _pdfDoc; }
            set { _pdfDoc = value; }
        }

        public override void ProduceReport()
        {
            PdfDoc = new pdfDocument("TV Guide", "BlueGhostTV.co.uk PC TV Guide");
            pdfTable PdfTable = new pdfTable();
            pdfPage PdfPage = PdfDoc.addPage();
            PdfTable.tableHeader.addColumn(new pdfTableColumn("Time", predefinedAlignment.csLeft, 200));
            PdfTable.tableHeader.addColumn(new pdfTableColumn("Program", predefinedAlignment.csCenter, 600));
            foreach (BTVChannel channel in Channels)
            {
                Dictionary<BTVSchedule, BTVProgram> filler = GetFiller(channel, 0);
                foreach (KeyValuePair<BTVSchedule, BTVProgram> fkvp in filler)
                {
                    BTVProgram fProg = fkvp.Value;
                    BTVSchedule fSched = fkvp.Key;
                    pdfTableRow myRow = PdfTable.createRow();
                    string sMin = fSched.StartTime.Minute.ToString();
                    if (sMin.Length != 2)
                        sMin = "0" + sMin;
                    myRow[0].columnValue = fSched.StartTime.Hour + ":" + sMin;
                    myRow[1].columnValue = fProg.Name + "," + fProg.GetShortSynopsis();
                    PdfTable.addRow(myRow); 
                }
                foreach (KeyValuePair<string, List<BTVSchedule>> kvp in channel.Schedules)
                {
                    foreach (BTVSchedule sched in kvp.Value)
                    {
                        BTVProgram prog = new BTVProgram();
                        if (channel.Programs.TryGetValue(sched.Pid, out prog))
                        {
                            pdfTableRow myRow = PdfTable.createRow();
                            string sMin = sched.StartTime.Minute.ToString();
                            if (sMin.Length != 2)
                                sMin = "0" + sMin;
                            myRow[0].columnValue = sched.StartTime.Hour + ":" + sMin;
                            myRow[1].columnValue = prog.Name + "," + prog.GetShortSynopsis();
                            PdfTable.addRow(myRow); 
                        }
                    }
                }
            }
            PdfPage.addTable(PdfTable, 800, 800);
        }

        public override void WriteReportToDisk(string path)
        {
            PdfDoc.createPDF(path);
        }
    }
}
